package com.mingqijia.gassafety.shared.utils.message;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.gap.basic.exception.BizException;
import com.mingqijia.gassafety.shared.utils.HttpUtil;
import com.mingqijia.gassafety.shared.utils.commonUtils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * @author zhangfei
 * @version 1.0.0
 * @ClassName AppMessageService.java
 * @createTime 2023年03月30日
 */
@Component
@Slf4j
public class AppMessageService {

    @Value(value = "${gassafety.app.appPush}")
    public String appPush = "http://push-service.st1.tgs.com";


    /**
     * app手机推送
     *
     * @param jsonData
     * @return
     */
    @Async
    public boolean sendSms(Map<String, Object> jsonData) {
        boolean result = false;
        log.info("AppMessageService-sendSms,主要发送数据：{}",
                JSONObject.toJSONString(jsonData));
        try {
            String body = HttpUtil.getBody(JSONObject.toJSONString(jsonData), appPush, "");
            log.info("AppMessageService-body数据：{}", body);
            if (StringUtils.isEmpty(body)) return false;
            JSONObject jsonObject = JSONObject.parseObject(body);
            JSONObject error = jsonObject.getJSONObject("error");
            if (error == null ||(jsonObject.containsKey("result")&& jsonObject.getBoolean("result"))) result = true;
        } catch (Exception e) {
            log.error("AppMessageService-error:{}", e);
            result = false;
        }
        return result;
    }

    /**
     * 参数封装
     *
     * @param alias 发送目标数组
     * @param text  发送内容
     * @param title 标题
     * @return
     */
    public Map<String, Object> getRequestParam(String[] alias, String text,String title) {
        if (StringUtils.isEmpty(title)||StringUtils.isEmpty(text)||alias.length<=0){
            throw new BizException("alias、text、title参数必填");
        }
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        List<Object> paramArray = new ArrayList<>();
        log.info("getRequestParam-param:{},{}", text, JSONObject.toJSONString(alias));
        params.put("jsonrpc", "2.0");
        params.put("method", "Push.send");
        param.put("platform", "all");
        param.put("castType", "customizedcast");
        param.put("aliasType", "towngas");
        param.put("alias", alias);
        param.put("priority", 1);
        param.put("displayType", "message");
        param.put("ticker", "");
        param.put("title", title);
        param.put("subtitle", "");
        param.put("custom", " {\"url\":\"\"}");
        param.put("description", "all-push");
        param.put("call_time", DateUtil.format(new Date(), DateUtils.DATE_TIME_FORMAT));
        param.put("afterOpen", "go_custom");
        param.put("text", text);
        paramArray.add(param);
        params.put("params", paramArray);
        return params;
    }

}


